import {baseUrl} from './config.js'
export function ajax(url,paraem={},type='POST',header = {'content-type':'application/x-www-form-urlencoded'}){
	return new Promise((resolve, reject) => {
		uni.showLoading({
			mask:true
		})
		if(uni.getStorageSync('userinfo')!=''){header.token=uni.getStorageSync('userinfo').token}
		uni.request({
			url:baseUrl+url,
			data:paraem,
			method:type,
			header:header,
			success(res) {
				uni.hideLoading()
				uni.stopPullDownRefresh()
				if(res.data.code==1){
					resolve(res.data);
				}else{
					// token过期
					if(res.data.message=='请先登录'){
						uni.showToast({
							title:'您的登陆已过期',
							icon:'none',
							duration:3000
						})
						setTimeout(function(){
							uni.reLaunch({
								url:'/pages/kitty-BasicLogin/login'
							})
						},1500)
						return
					}
					uni.showToast({
						title:res.data.message||'连接服务器失败,请稍后重试!',
						icon:'none',
						duration:3000
					})
					reject(res.data)
				}
			},
			fail(e) {
				uni.showToast({
					title:'连接失败,请检查网络后再试!',
					icon:'none'
				})
				uni.hideLoading()
			}
		})
	})
}